package com.guiji.quartz.domain;

import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DateFormatUtil;
import com.guiji.quartz.util.DelPicUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @date 2022/1/28 11:41
 */
public class SouhuTiyu {
	private static final Logger log = LoggerFactory.getLogger(SouhuTiyu.class);

	public static void mainMethod() throws Exception {
		HttpResponse response = null;
		try {
			response = HttpRequest.get("https://sports.sohu.com/?spm=smpc.fb-sports-home.top-logo.1.164334032046813Yee4a")
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
				.header("Accept-Encoding", "gzip, deflate")
				.setConnectionTimeout(15000)
				.execute();
		} catch (Exception e) {
			log.error("代理失败1" + e);
			return;
		}
		String resp = response.body();
		Set<String> resUrl = getUrl(resp);
		getContent(resUrl);

	}

	private static void getContent(Set<String> listContent) {
		List<Map> allNews = new ArrayList();
		for (String url : listContent) {
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc;
			JXDocument jxDocument;
			try {
				HttpResponse response = HttpRequest.get(url).setConnectionTimeout(15000).execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("代理失败2" + e);
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1/span[@class='title-info-title']/text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}

			List<Object> newImgs = null;
			try {
				newImgs = jxDocument.sel("//article[@class='article']/p/img/@src");
			} catch (Exception e) {
				log.error("获取图片失败");
			}

			//阅读量
			Integer readNum;
			try {
				String id = url.substring(23,32);
				//https://v2.sohu.com/public-api/articles/519531830/pv?callback=jQuery112408725929713039917_1643355105832&_=1643355105833
				String readUrl = "https://v2.sohu.com/public-api/articles/"+id+"/pv?callback=jQuery112408725929713039917_1643355105832&_="+System.currentTimeMillis();
				String body = HttpRequest.get(readUrl).setConnectionTimeout(15000).execute().body();
				readNum = Integer.parseInt(ReUtil.findAll("\\([0-9]*?\\)",body,0).get(0).replaceAll("\\)","").replaceAll("\\(",""));
			} catch (Exception e) {
				continue;
			}

			Document document = null;
			String cont = null;
			StringBuffer content = new StringBuffer();
			try {
				for (int i = 1; i <= newImgs.size(); i++) {
					doc = doc.replaceAll(newImgs.get(i-1).toString(), "<p>#" + i + "#</p>");
				}
				document = Jsoup.parse(doc);
				cont = document.getElementsByClass("article").get(0).toString().replaceAll("\n","").replaceAll("返回搜狐，查看更多","");
				ReUtil.findAll("<p>.*?</p>",cont,0).forEach(s -> {
					content.append(s.replaceAll("<.*?>","").replaceAll("&nbsp;"," ")+"$$$");
				});
			} catch (Exception e) {
				continue;
			}

			Date date = new Date();

			if (content.length() > 50000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || newImgs.size() > 20) {
				continue;
			}

			try {
				SpiderNews spiderNews = new SpiderNews();
				spiderNews.setNewsTitle(title);
				spiderNews.setNewsContent(content.toString());
				spiderNews.setNewsPicJson(JSONUtil.toJsonStr(newImgs));
				spiderNews.setNewsUrl(url);
				spiderNews.setNewsPublicTime(date);
				HashMap map = new HashMap();
				map.put("spidernews", spiderNews);
				map.put("hotData", readNum);
				allNews.add(map);
			} catch (Exception e) {
				continue;
			}

		}

		//对新闻热度排序
		Collections.sort(allNews, (o1, o2) -> (int) o2.get("hotData") - (int) o1.get("hotData"));
		int k = 15;
		if (allNews.size() < 15) {
			k = allNews.size();
		}
		for (int i = 0; i < k; i++) {
			Map map1 = allNews.get(i);
			SpiderNews sp = (SpiderNews) map1.get("spidernews");
			try {
				DataSaveUtil.saveData(sp.getNewsTitle(), sp.getNewsContent(), sp.getNewsPicJson(), sp.getNewsUrl(), sp.getNewsPublicTime(), "https://sports.sohu.com/", "搜狐-体育");
				log.info(Thread.currentThread().getName() + "-----------------搜狐-体育----------------");
			} catch (Exception e) {
				continue;
			}
		}

	}

	private static Set<String> getUrl(String content) {

		Set<String> res = new HashSet<>();
		String pattern = "https://www.sohu.com/a/.*?\\?scm=.*?\"";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add(m.group().replaceAll("\"",""));
		}
		return res;
	}

	public static void main(String[] args) throws Exception {
//		Set<String> res = new HashSet<>();
//		res.add("https://www.sohu.com/a/519533289_461392?scm=1004.727939458711879680.0.0.0&spm=smpc.fb-sports-home.content1-n-5.4.16433407854561963nI5");
//		getContent(res);
		mainMethod();
	}
}
